package com.wangjie.common;

import lombok.Data;

@Data
public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    private static <T> R<T> build(T data) {
        R<T> r = new R<>();
        if (data != null) {
            r.setData(data);
        }
        return r;
    }

    public static <T> R<T> build(T data, Integer code, String msg) {
        R<T> r = build(data);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> ok(T data) {
        return build(data, 200, "success");
    }

    public static R ok() {
        return R.ok(null);
    }

    public static <T> R<T> error() {
        return R.error(500, "error");
    }

    public static <T> R<T> error(Integer code, String msg) {
        R<T> r = build(null);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
}